package com.example.bjsfpost.emails;

import com.alibaba.fastjson.JSONObject;
import com.example.bjsfpost.utils.HttpUtil;
import com.example.bjsfpost.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.support.MultipartFilter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class EmailServiceImpl implements EmailService {

    @Value("${bjsf.email.url:}")
    private String bjsfEmailUrl = "";
    @Value("${bjsf.email.secret:}")
    private String bjsfEmailSecret = "";
    @Value("${bjsf.email.appname:}")
    private String bjsfEmailAppName = "";
    private static final String BJSF_TOKEN_EXPIRE = "调用错误 msg:token过期";
    private static volatile String BJSF_TOKEN = "";

    @Override
    public BjsfEmailResponseVO sendEmail(EmailRequestDTO req, List<MultipartFilter> files) {
        BjsfEmailResponseVO bjsfEmailResponseVO = tryHttpRequest(req,false);
        if (Objects.nonNull(bjsfEmailResponseVO) && !bjsfEmailResponseVO.getSuccess() && BJSF_TOKEN_EXPIRE.equals(bjsfEmailResponseVO.getMessage())) {
            bjsfEmailResponseVO = tryHttpRequest(req,true);
        }
        return bjsfEmailResponseVO;
    }

    /**
     * 是否是token过期
     *
     * @param req
     * @return
     */
    private BjsfEmailResponseVO tryHttpRequest(EmailRequestDTO req,Boolean refreshToken) {
        //String url = "http://192.188.2.211/platform/email/email/send";
        Map<String, String> header = new HashMap<>();
        header.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//        header.put("Content-Type","multipart/form-data;boundary=something");
        header.put("token", generatorToken(refreshToken));
        Map<String, String> body = new HashMap<>();
        body.put("subject", "【测试】测试BJSF-04 邮件发送");
        body.put("htmlContent", "hello word! https://www.baidu.com");
        body.put("tos", "wangqiang_it@sf.local");
//        body.put("ccs","");
        body.put("env", "TEST");
//        body.put("flag","");
//        body.put("files","D:\\JAVAPLM二开文档\\email.txt");
        String resp = HttpUtil.sendPost(bjsfEmailUrl, header, body);
        BjsfEmailResponseVO bjsfEmailResponseVO = null;
        if (StringUtils.hasText(resp)) {
            try {
                bjsfEmailResponseVO = JSONObject.parseObject(resp, BjsfEmailResponseVO.class);
            } catch (Exception e) {
                log.error("json bjsfEmailResponseVO error");
            }
        }
        return bjsfEmailResponseVO;
    }

    private String generatorToken(Boolean nowFlag) {
        if (!nowFlag) {
            if (StringUtils.hasText(BJSF_TOKEN)) {
                return BJSF_TOKEN;
            }
        }
        log.info("refresh bjsf token");
        return BJSF_TOKEN = JwtUtils.apiCreateAppToken(bjsfEmailSecret, bjsfEmailAppName);
    }
}
